/*
 * MessageInterpreter.cpp
 *
 *  Created on: Oct 21, 2008
 *      Author: Ryan
 */

#include "MessageInterpreter.h"
#include "../OOInterface/Objects/Logger.h"

#include <sstream>

#include "Sim/Features/FeatureDef.h"

MessageInterpreter::MessageInterpreter()
{
}

MessageInterpreter::~MessageInterpreter()
{
}

static const int MAP = 1;
static const int MOVE = 2;
static const int GAIA = 3;
static const int UNITCOUNT = 4;
static const int DRAW = 5;
static const int UNITPRINT = 6;
static const int FEATUREPRINT = 7;


void MessageInterpreter::interpret( std::string str, IAICallback* cb, SpringAIInterface* i )
{
	int token = tokenizeString(str);

	OOInterface::Logger& log = OOInterface::Logger::getInstance("AI.txt");

	log << str << std::endl;
	log << "--------------------------------------------------------" << std::endl;



	switch( token ) {
	case MAP :
	{
		//const OOInterface::GameMap& map = i->getGameMap();

		std::ostringstream string;



		i->sendTextMessage(string.str());

	}
		break;

	case DRAW :
	{
		const OOInterface::GameMap& map = i->getGameMap();
		const OOInterface::ResourceList& resources = i->getResourceTypes();


		for( unsigned int y = 0; y < map.getHeight(); ++y)
		{
			for( unsigned int x = 0; x < map.getWidth(); ++x)
			{
				if(map.getResourceDensity(resources.at(1),x,y) > 0.0f)
				{
					float3 pos( x*8, map.getElevation(x, y) + 10.0f, y*8);
					cb->DrawUnit( "AMGEO", pos, 0.0f, 360, 1, false, true );
				}

				if(map.getResourceDensity( resources.at(0),x,y) > 0.0f)
				{
					float3 pos( x*8, map.getElevation(x,y) + 10.0f, y*8);

					if( map.getResourceDensity( resources.at(0),x,y) > 0.5f)
						cb->DrawUnit( "ARMMINE2", pos, 0.0f, 360, 1, false, true);
					else
						cb->DrawUnit("ARMMINE1", pos, 0.0f, 360, 1, false, true);
				}
			}
		}
	}
		break;

	case MOVE :



		//OOInterface::Command command = i->getCommandFactory()->createMoveCommand(new OOInterface::Point3D(width/2, 0.0f, height/2));

		//SpringCommand* cmd = dynamic_cast<SpringCommand*> ( command );
		break;

	case GAIA:
	{

		int* unitIDs = new int[10000];
		int nTotal = cb->GetNeutralUnits( unitIDs );
		int eTotal = cb->GetEnemyUnits( unitIDs );
		int fTotal = cb->GetFriendlyUnits( unitIDs );

		int features = cb->GetFeatures( unitIDs, 10000, float3( 0.0f, 0.0f, 0.0f), 100000.0f );

		std::ostringstream string;

		string << "Neutral: " << nTotal << " Enemy: " << eTotal << " Friendly: " << fTotal << " Features: " << features;

		i->sendTextMessage( string.str() );

		delete unitIDs;
	}
		break;

	case FEATUREPRINT:
	{
		int* featureIDs = new int[10000];

		// The current getFeatures is broken in AICallback.cpp :((((
		//int total = cb->GetFeatures( featureIDs, 9999 );


		//Pathetic hack around the bugged getFeatures( ... )
		float3 pos( 0.0f, 0.0f, 0.0f);
		int total = cb->GetFeatures( featureIDs, 9999, pos, 100000.0f );

		OOInterface::Logger& log = OOInterface::Logger::getInstance("AI.txt");

		log << "FeatureIDs: " << total << std::endl;

		for( int i =0; i < total; ++i )
		{

			const FeatureDef* def = cb->GetFeatureDef( featureIDs[i] );
			log << i << "Name: " << def->myName << " " << def->description << std::endl;
			float metal = def->metal; float energy = def->energy;
			log << i << "Metal: " << metal << " Energy: " << energy << std::endl;
		}
	}
	break;
	default:
		cb->SendTextMsg("Unrecognized message.", 0);
	}

	log << "--------------------------------------------------------" << std::endl;
}

int MessageInterpreter::tokenizeString( std::string msg )
{

	if( msg.compare("map") == 0)
	{
		return MAP;
	}
	else if ( msg.compare("move") == 0 )
	{
		return MOVE;
	}
	else if ( msg.compare("gaia") == 0 )
	{
		return GAIA;
	}
	else if (msg.compare("unitcount") == 0 )
	{
		return UNITCOUNT;
	}
	else if (msg.compare("draw") == 0)
	{
		return DRAW;
	}
	else if (msg.compare("unitprint") == 0)
	{
		return UNITPRINT;
	}
	else if (msg.compare("featureprint") == 0)
	{
		return FEATUREPRINT;
	}

	return -1;
}
